package com.wyp168.leetcode;

/**
 * @ClassName IsValidSudokuSolution
 * @Title TODO
 * @Description TODO
 * @Author wyp168
 * @Date 2022/10/21 21:10
 */
public class IsValidSudokuSolution {
    public static void main(String[] args) {
        IsValidSudokuSolution solution = new IsValidSudokuSolution();
//        char[][] board = {{'5','3','.','.','7','.','.','.','.'},{'6','.','.','1','9','5','.','.','.'}, {'.','9','8','.','.',".",".","6","."}, {"8",".",".",".","6",".",".",".","3"},{"4",".",".","8",".","3",".",".","1"},{"7",".",".",".","2",".",".",".","6"}, {".","6",".",".",".",".","2","8","."},{".",".",".","4","1","9",".",".","5"},{".",".",".",".","8",".",".","7","9"}}
    }

    public boolean isValidSudoku(char[][] board) {
        int[][] rows = new int[9][9];
        int[][] cols = new int[9][9];
        int[][][] boxs = new int[3][3][9];

        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                char c = board[i][j];
                if (c != '.') {
                    int index = c - '0';
                    rows[i][index] ++;
                    cols[j][index] ++;
                    boxs[i / 3][j / 3][index] ++;
                    if (rows[i][index] > 1 || cols[j][index] > 1 || boxs[i / 3][j / 3][index] > 0) {
                        return false;
                    }
                }
            }
        }
        return true;
    }
}
